package com.snsoft.service;

import static com.snsoft.util.Constants.*;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.SQLException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.snsoft.bean.ResultMap;
import com.snsoft.bean.User;
import com.snsoft.bean.UserSession;
import com.snsoft.dao.UserDao;
import com.snsoft.exception.PasswordErrorException;
import com.snsoft.exception.UserNotFoundException;
import com.snsoft.util.Constants;
import com.snsoft.util.EncodeUtils;

/**
 * 
 * @author 慎伟康
 * 
 * @date 2017年11月26日 下午4:16:43
 * 
 * @Description TODO
 *    user模块service
 */
@Service  //声明为service
public class UserService {

	@Autowired
	private UserDao userDao;
	
	/**
	 * 登录接口方法，获取用户
	 * 
	 * @param userCode
	 * @return User
	 * 
	 * @throws SQLException
	 * @throws UserNotFoundException
	 * @throws UnsupportedEncodingException 
	 */
	public ResultMap loginService(String userCode, String userPassWord, int autoLoginTime,
			HttpSession session, HttpServletResponse response) 
					throws SQLException, UserNotFoundException, UnsupportedEncodingException {
		User user = userDao.getUser(userCode);
		if(user == null){
			throw new UserNotFoundException(Constants.MSG_SERVICE_USERNOTFOUND + "userCode=" + userCode);
		}
		
		//校验密码
		if(!EncodeUtils.encodeByMd5(userPassWord).equals(user.getUserPassWord())){
			return new ResultMap(CODE_ERROR, MSG_SERVICE_PASSWORDERROR, null);
		}
		
		//密码正确，写入session
		session.setAttribute("userSession", 
				new UserSession(user.getUserID(), user.getUserName(), user.getRole()));
		
		//拼接账号和密码，密码加密
		String userInfo = user.getUserCode() + "_"
				+ EncodeUtils.encodeByBase64(user.getUserPassWord());
		//写入cookie，URL编码解决中文乱码
		Cookie cookie = new Cookie("userInfo", URLEncoder.encode(userInfo, CHARSET));
		cookie.setMaxAge(60 * 60 * 24 * autoLoginTime);//设置cookie有效期，以天为单位。
		response.addCookie(cookie);
		
		return new ResultMap(CODE_SUCCESS, MSG_OPERATE_SUCCESS, user);
	}
	
	/**
	 * 修改密码接口方法，用户修改密码
	 * 
	 * @param userID
	 * @param oldPassWord
	 * @param newPassWord
	 * 
	 * @throws SQLException
	 * @throws UserNotFoundException
	 * @throws PasswordErrorException
	 */
	public void updatePasswordService(int userID, String oldPassWord, String newPassWord) 
			throws SQLException, UserNotFoundException, PasswordErrorException {
		User user = userDao.getUser(userID);
		
		//旧密码校验
		if(!user.getUserPassWord().equals(EncodeUtils.encodeByMd5(oldPassWord))){
			throw new PasswordErrorException(Constants.MSG_SERVICE_PASSWORDERROR + "userID=" + userID);
		}else{//旧密码正确，可修改密码
			userDao.updatePassword(userID, EncodeUtils.encodeByMd5(newPassWord));
		}
	}
	
	/**
	 * 声明式事务管理样例，基于方法，粒度不能再细
	 * 
	 * @Transactional 有许多属性可以设置，可个性化配置。
	 * @param userID
	 * @param newPassWord
	 * @throws SQLException
	 */
	@Transactional
	public void transactionManagerDemo(int userID, String newPassWord) throws SQLException {
		userDao.updatePassword(userID, newPassWord);
		throw new RuntimeException("test");
//		System.out.println(1/0);
	}
}
